<script setup lang="ts">
import { del, getApiPublishList } from '@/api/api-manage/my-approve-records'
import { useRenderIcon } from '@/components/ReIcon/src/hooks'

import { usePagination } from '@/hooks/usePagination'
import useDictStore from '@/store/modules/dict'
import { ModalShowType } from '@/types/common'
import { formatDate, omitEmptyValue } from '@/utils/common'
import { message } from '@/utils/message'
import apiApplicationModal from '@/views/api-manage/my-apply/api-application-modal.vue'
import Refresh from '@iconify-icons/ep/refresh'
import Search from '@iconify-icons/ep/search'
import { FormInstance } from 'element-plus'
import { isNil } from 'lodash-es'
import { computed, onMounted, reactive, ref } from 'vue'

defineOptions({
  name: 'api-manage/list-publish',
})

const router = useRouter()
const queryParams = reactive({
  applyName: '',
  applyStatus: '',
})
const searchDateRange = ref<[string, string]>(['', ''])
const searchDateRangeParams = computed(() =>
  !isNil(searchDateRange.value)
    ? {
        'params[beginApplySubmitTime]': searchDateRange.value[0],
        'params[endApplySubmitTime]': searchDateRange.value[1],
      }
    : null
)
const { pagination, paginationParams, changePageNumber, changePageSize } = usePagination(() =>
  onSearch()
)
const formRef = ref<FormInstance>()
const dictStore = useDictStore()
const { api_apply_status } = dictStore.getDicts('api_apply_status')
const already_status = [
  { label: '审核通过', value: '1' },
  { label: '审核拒绝', value: '2' },
]
const columns: TableColumnList = [
  {
    type: 'selection',
    width: 55,
    align: 'left',
  },
  {
    label: '申请单编号',
    prop: 'applyNo',
    minWidth: 100,
  },
  {
    label: '申请单名称',
    prop: 'applyName',
    minWidth: 150,
  },
  {
    label: '申请人',
    prop: 'applyPerson',
    minWidth: 150,
  },
  // {
  //   label: "申请类型",
  //   prop: "applyType",
  //   minWidth: 100,
  //   formatter({ applyType }) {
  //     return api_apply_type.getLabel(applyType);
  //   }
  // },
  {
    label: '申请提交时间 ',
    prop: 'applyStatus',
    minWidth: 180,
    formatter: ({ applySubmitTime }) => formatDate(applySubmitTime),
  },
  {
    label: '申请状态',
    prop: 'type',
    minWidth: 100,
    formatter({ applyStatus }) {
      return api_apply_status.getLabel(applyStatus)
    },
  },
  {
    label: '审批人',
    prop: 'userName',
    minWidth: 100,
    formatter({ userName }) {
      if (userName == '' || userName == null) {
        userName = '免审批'
      }
      return userName
    },
  },
  {
    label: '操作时间 ',
    prop: 'oprateTime',
    minWidth: 180,
    formatter: ({ oprateTime }) => formatDate(oprateTime),
  },
  {
    label: '操作',
    fixed: 'right',
    width: 180,
    slot: 'operation',
  },
]
const dataList = ref([])
const loading = ref(false)
/** 查询列表 */
async function onSearch() {
  loading.value = true
  const { rows, total } = await getApiPublishList(
    omitEmptyValue({
      applyName: queryParams.applyName,
      ...searchDateRangeParams.value,
      applyStatus: queryParams.applyStatus,
      ...paginationParams.value,
    })
  )
  dataList.value = rows
  pagination.total = total

  setTimeout(() => {
    loading.value = false
  }, 500)
}

/** 重置新增的表单  */
async function resetForm() {
  formRef.value.resetFields()
  queryParams.applyName = ''
  queryParams.applyStatus = ''
  searchDateRange.value = ['', '']
  await onSearch()
}

onMounted(async () => {
  await onSearch()
})
const modalShow = ref(ModalShowType.Hide)
const termId = ref<number | null>(null)
</script>

<template>
  <div class="main table-view-layout">
    <el-form
      ref="formRef"
      :inline="true"
      :model="queryParams"
      class="bg-el-bg w-[99/100] pl-8 pt-4"
    >
      <el-form-item>
        <el-input
          v-model="queryParams.applyName"
          placeholder="请输入申请关键词"
          clearable
          style="width: 240px"
          @keyup.enter="onSearch()"
        />
      </el-form-item>
      <el-form-item label="申请提交时间" prop="searchDateRange">
        <el-date-picker
          v-model="searchDateRange"
          unlink-panels
          type="datetimerange"
          value-format="YYYY-MM-DD HH:mm:ss"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="queryParams.applyStatus"
          placeholder="申请状态"
          clearable
          class="!w-[180px]"
        >
          <el-option
            v-for="item in already_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch()"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm"> 重置 </el-button>
      </el-form-item>
    </el-form>
    <PureTableBar>
      <template #table>
        <pure-table
          border
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :data="dataList"
          :columns="columns"
          :pagination="pagination"
          :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)',
          }"
          @page-size-change="changePageSize"
          @page-current-change="changePageNumber"
        >
          <template #operation="{ row }">
            <el-button
              link
              class="reset-margin"
              type="primary"
              @click="
                () => {
                  router.push({
                    path: '/api-manage/detail',
                    query: { id: row.id },
                  })
                }
              "
            >
              接口详情
            </el-button>
            <!-- <ElPopconfirm
              title="是否确认删除?"
              @confirm="
                () => {
                  del(row.id).then(() => {
                    message('删除成功', { type: 'success' })
                  })
                  onSearch()
                }
              "
            >
              <template #reference>
                <el-button
                  link
                  class="reset-margin"
                  type="danger"
                  @click="
                    () => {
                      modalShow = ModalShowType.Hide
                      termId = row.applyId
                    }
                  "
                >
                  删除
                </el-button>
              </template>
            </ElPopconfirm> -->
          </template>
        </pure-table>
      </template>
    </PureTableBar>
    <apiApplicationModal
      v-model:modalShow="modalShow"
      :term-id="termId"
      :on-search="() => onSearch()"
    />
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}
</style>
